package com.ruoyi.app.controller;

import com.ruoyi.app.common.jwt.TokenService;
import com.ruoyi.app.service.UserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.app.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户相关")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    UserService userService;
    @Autowired
    TokenService tokenService;
    @Autowired
    RedisCache redisCache;

    @ApiOperation("微信一键登录")
    @PostMapping("/wx-login/{code}")
    public AjaxResult wxLogin(@PathVariable("code")String code, @RequestParam("encrypted") String encrypted,
                              @RequestParam("iv") String iv,String name,String icon,@RequestParam String appId){
        return userService.wxLogin(code,encrypted,iv,name,icon,appId);
    }

    @ApiOperation("验证码登录")
    @PostMapping("/sms-login")
    public AjaxResult smsLogin(@RequestParam String phone,@RequestParam String code,@RequestParam String appId){

        return AjaxResult.success(userService.smsLogin(phone, code,appId));
    }

    @ApiOperation("验证验证码")
    @PostMapping("/sms-check")
    public AjaxResult checkSms(@RequestParam String phone,@RequestParam String code){
        String redisCode = redisCache.getCacheObject("register:phone" + phone);
        if (StringUtils.isEmpty(redisCode) || !code.equals(redisCode)) {
            throw new ServiceException("验证码错误");
        }
        return AjaxResult.success();
    }

    @ApiOperation("修改手机号")
    @PostMapping("/set-phone")
    public AjaxResult setPhone(@RequestParam String phone,@RequestParam String code){
        userService.setPhone(phone, code);
        return AjaxResult.success();
    }

    @ApiOperation("发送验证码")
    @GetMapping("/send-sms")
    public AjaxResult sendSms(@RequestParam String phone){

        return userService.sendSms(phone);
    }

    @ApiOperation("编辑用户名和头像")
    @PutMapping("/user-icon")
    public AjaxResult setUser( String name,String icon){
        User user = tokenService.getUser(ServletUtils.getRequest());
        User user1 = new User();
        user1.setNickName(name);
        user1.setIcon(icon);
        user1.setId(user.getId());
        userService.setUser(user1);
        return AjaxResult.success();
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public AjaxResult getUserInfo(){
        return AjaxResult.success(userService.getUserInfo());
    }

    @ApiOperation("获取电卡列表")
    @GetMapping("/card-list")
    public AjaxResult cardList(){
        startPage();
        return AjaxResult.success(getDataTable(userService.cardList()));
    }

    @ApiOperation("获取钱包列表")
    @GetMapping("/balance-list")
    public AjaxResult balanceList(Long communityId){
        startPage();
        return AjaxResult.success(getDataTable(userService.balanceList(communityId)));
    }

    @ApiOperation("挂失电卡 type=0挂失 1取消挂失")
    @PutMapping("/loss-card")
    public AjaxResult lossCard(@RequestParam Long cardId){
        userService.lossCard(cardId);
        return AjaxResult.success();
    }

    @ApiOperation("解绑电卡")
    @PutMapping("/unbind-card")
    public AjaxResult unbindCard(@RequestParam Long cardId){
        userService.unbindCard(cardId);
        return AjaxResult.success();
    }

    @ApiOperation("绑定电卡")
    @PutMapping("/bind-card")
    public AjaxResult bindCard(@RequestParam String cardNum){
        userService.bindCard(cardNum);
        return AjaxResult.success();

    }

    @ApiOperation(value = "获取当前用户是否正在充电",notes = "根据 state显示不同状态 1充电中 2寄存中  存在这三个状态的订单 无法再次充电")
    @GetMapping("/recharge-status")
    public AjaxResult getRechargeStatus(){
        return AjaxResult.success(userService.getRechargeStatus());
    }


}
